package baseTest;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

public class findAnagrams {
    public static void main(String[] args) {
        String s = "cbaebabacd";
        String p = "abc";
        System.out.println(process(s,p));
    }
    public static List<Integer> process(String s,String p){
        int[] pArr = new int[26];
        int[] winArr = new int[26];
        List<Integer> list = new ArrayList<>();

        int left = 0;
        int right = 0;
        int sLen = s.length();
        int pLen = p.length();
        if (s.length() < p.length()){
            return list;
        }

        for (; right < p.length(); right++) {
            pArr[p.charAt(right) - 'a']++;
            winArr[s.charAt(right) - 'a']++;
        }

        if (Arrays.equals(pArr,winArr)){
            list.add(0);
        }

        for (; right<sLen ; right++) {
            winArr[s.charAt(right-pLen)-'a']--;
            winArr[s.charAt(s.charAt(right)-'a')]++;
            if (Arrays.equals(winArr,pArr)){
                list.add(right-pLen+1);
            }
        }
        return list;
    }
}
